package com.innjia.sys.controller;

import com.innjia.base.annotation.SysLog;
import com.innjia.base.exception.ValidataException;
import com.innjia.base.utils.EncryptionUtils;
import com.innjia.base.utils.Result;
import com.innjia.sys.entity.UserEntity;
import com.innjia.sys.entity.UserLandlordEntity;
import com.innjia.sys.service.UserLandlordService;
import com.innjia.sys.service.UserRoleService;
import com.innjia.sys.service.UserService;
import com.xiaoleilu.hutool.lang.Validator;
import com.xiaoleilu.hutool.util.IdcardUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 房东用户表
 *
 * @author innjia
 * @date 2017-05-03 09:41:38
 */
@RestController
@RequestMapping("sys/userLandlord")
public class UserLandlordController extends AbstractUserController {
    @Autowired
    private UserLandlordService userService;

    @Autowired
    private UserRoleService userRoleService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:user:list")
    @SysLog("查看系统用户列表")
    public Result list(@RequestParam Map<String, Object> params) {
        return super.list(params, null);
    }

    @Override
    protected UserService getUserService() {
        return userService;
    }

    @Override
    protected UserRoleService getUserRoleService() {
        return userRoleService;
    }

    /**
     * 禁用、
     */
    @Override
    @RequestMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    @SysLog("禁用系统用户")
    public Result delete(@RequestBody String[] ids) {
        return super.delete(ids);
    }

    /**
     * 启用、
     */
    @Override
    @RequestMapping("/enabled")
    @RequiresPermissions("sys:user:enabled")
    @SysLog("启用系统用户")
    public Result enabled(@RequestBody String[] ids) {
        return super.enabled(ids);
    }

    /**
     * 重置密码
     */
    @Override
    @RequestMapping("/reset")
    @RequiresPermissions("sys:user:reset")
    @SysLog("重置密码")
    public Result reset(@RequestBody String[] ids) {
        return super.reset(ids);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:user:info")
    @SysLog("查看系统用户信息")
    public Result info(@PathVariable("id") String id) {
        return super.info(id, null);
    }

    /**
     * 主页用户信息
     */
    @RequestMapping("/info")
    public Result info() {
        return super.info(null);
    }

    /**
     * 根据手机号查询房东
     */
    @RequestMapping("/findByPhone/{phone}")
    public Result findByPhone(@PathVariable("phone") String phone) {
        UserLandlordEntity userEntity = (UserLandlordEntity) userService.queryByPhone(phone);
        annon(userEntity);
        return Result.ok().put("userlandlord", userEntity);
    }

    /**
     * 脱敏
     *
     * @param user
     */
    private void annon(UserEntity user) {
        if (null != user) {
            EncryptionUtils.anno(user, "idCard", EncryptionUtils.TYPE_ID_CARD);
            EncryptionUtils.anno(user, "phone", EncryptionUtils.TYPE_MOBILE);
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody UserLandlordEntity user) {
        try {
            validata(user);
        } catch (ValidataException e) {
            return e.getResult();
        }
        return super.save(user);
    }

    private void validata(@RequestBody UserLandlordEntity user) throws ValidataException {
        if (StringUtils.isBlank(user.getIdCard()) || !IdcardUtil.isValidCard(user.getIdCard())) {
            throw new ValidataException(Result.error("身份证号码有误"));
        }
        if (StringUtils.isBlank(user.getPhone()) || !Validator.isMobile(user.getPhone())) {
            throw new ValidataException(Result.error("手机号码有误"));
        }
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @RequiresPermissions("sys:user:update")
    @SysLog("修改系统用户")
    public Result update(@RequestBody UserLandlordEntity user) {
        try {
            validata(user);
        } catch (ValidataException e) {
            return e.getResult();
        }
        if (null != user.getIdCard() && user.getIdCard().contains(EncryptionUtils.ANON_STR)) {
            user.setIdCard(null);
        }
        if (null != user.getPhone() && user.getPhone().contains(EncryptionUtils.ANON_STR)) {
            user.setPhone(null);
        }
        return super.update(user, userEntity -> {
            //脱敏后的数据不要保存
            UserLandlordEntity userLandlordEntity = (UserLandlordEntity) userEntity;
            if (null != userLandlordEntity.getPhone() && userLandlordEntity.getPhone().contains(EncryptionUtils.ANON_STR)) {
                userLandlordEntity.setPhone(null);
            }
            if (null != userLandlordEntity.getIdCard() && userLandlordEntity.getIdCard().contains(EncryptionUtils.ANON_STR)) {
                userLandlordEntity.setIdCard(null);
            }
        });
    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @SysLog("用户修改密码")
    public Result updatePassword(UserLandlordEntity user) {
        return super.updatePassword(user);
    }

}
